package com.opd.soeghtool;

public class ByteDataUtils {
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static byte[] getHexBytes(String message) {
        int len = message.length() / 2;
        char[] chars = message.toCharArray();

        String[] hexStr = new String[len];

        byte[] bytes = new byte[len];

        for (int i = 0, j = 0; j < len; i += 2, j++) {
            hexStr[j] = "" + chars[i] + chars[i + 1];
            bytes[j] = (byte) Integer.parseInt(hexStr[j], 16);
        }
        return bytes;
    }

    public static int bytesToIntMSB(byte []a, int start) {
        int s = 0;
        int s0 = a[start] & 0xff;
        int s1 = a[start + 1] & 0xff;
        int s2 = a[start + 2] & 0xff;
        int s3 = a[start + 3] & 0xff;
        s0 <<= 24;
        s1 <<= 16;
        s2 <<= 8;
        s = s0 | s1 | s2 | s3;
        return s;
    }

    public static int bytesToIntLSB(byte[] a, int start) {
        int s = 0;
        int s0 = a[start + 3] & 0xff;
        int s1 = a[start + 2] & 0xff;
        int s2 = a[start + 1] & 0xff;
        int s3 = a[start ] & 0xff;
        s0 <<= 24;
        s1 <<= 16;
        s2 <<= 8;
        s = s0 | s1 | s2 | s3;
        return s;
    }
}
